package com.whfp.coky.ui.task.frag

import android.os.Bundle
import android.view.View
import com.hjq.widget.view.SwitchButton
import com.whfp.coky.databinding.FragFluorSettingsBinding
import com.whfp.coky.other.Constants
import com.whfp.coky.other.DialogBuilder
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseFragment
import com.whfp.coky.ui.task.act.FluorMainAct
import com.whfp.coky.ui.task.act.fluor.SeniorAct
import com.whfp.coky.ui.task.act.fluor.ThresholdListAct
import com.whfp.coky.ui.task.model.FluorSettingBean
import com.whfp.coky.ui.task.vm.fluor.FluorSettingsFragVM
import com.whfp.coky.utils.AESUtils

/**
 *
 *
 * created by coky on 2022/3/16
 */
class FluorSettingsFrag :BaseFragment<FluorSettingsFragVM, FragFluorSettingsBinding>(),SwitchButton.OnCheckedChangeListener{

    companion object{
        fun newInstance(bean : FluorSettingBean?): FluorSettingsFrag {
            return FluorSettingsFrag().apply {
                bean?.let {
                    arguments = Bundle().apply {
                        putParcelable(Constants.BEAN,it)
                    }
                }
            }
        }
    }


    override fun initView() {
//        vModel.curveSets = resources.getStringArray(R.array.curve_type).toMutableList()
        vModel.doLoadSettings(this,arguments?.getParcelable(Constants.BEAN))
    }

    override fun initData() {
        setOnClickListener(vBinding.sb01,vBinding.sb02,vBinding.sb03,
            vBinding.sb04,vBinding.sb05)
    }


    override fun onClick(view: View) {
        super.onClick(view)
        when(view){
            vBinding.sb01->(requireActivity() as BaseActivity<*, *>).startActivity(ThresholdListAct::class.java)
            vBinding.sb02->showPwdInputDialog()
            vBinding.sb03->vModel.doShowCurveSelect(requireContext())
            vBinding.sb04->vModel.doDeviceAutoCheck()
            vBinding.sb05->vModel.doContactUs(requireActivity() as BaseActivity<*, *>)
        }
    }

    private fun showPwdInputDialog() {
        DialogBuilder.inputPwdDialog(requireContext(),"管理员密码",""){
            if (AESUtils.decryptAdministratorPassword(it!!)) {
                (requireActivity() as BaseActivity<*, *>).startActivity(SeniorAct::class.java)
            } else {
                toast("管理员密码有误！")
            }
        }
    }

//    按钮点击联动setting变化
    override fun onCheckedChanged(button: SwitchButton, checked: Boolean) {
            button.tag = checked
            when(button) {
                vBinding.sw01 -> {
                    (getAttachActivity() as FluorMainAct).getSettings().isCoverOldProj = checked
                    vModel.inputText(0, checked)
                }
                vBinding.sw02 -> {
                    (getAttachActivity() as FluorMainAct).getSettings().isShowResult = checked
                    vModel.inputText(1, checked)
                }
                vBinding.sw03 -> {
                    (getAttachActivity() as FluorMainAct).getSettings().detecType = checked
                    vModel.inputText(2, checked)
                }
                vBinding.sw04 -> {
                    (getAttachActivity() as FluorMainAct).getSettings().lowUpCaculate = checked
                    vModel.inputText(3, checked)
                }
                vBinding.sw05 -> {
                    (getAttachActivity() as FluorMainAct).getSettings().isAlarm = checked
                    vModel.inputText(4, checked)
                }
            }
    }

}